package face.yuque;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author spf
 * @date 2024/12/12
 * @time 20:24
 */

//两数之和
//方法二：使用Map
public class TwoSum2 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 6, 5};
        int target = 10;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }

    private static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[]{0};
    }

}
